﻿/* --------------------------------------------------------------
 * exMath (C) Laurens Ruijtenberg
 * 
 * This code is being distributed under the MS-PL license. Feel
 * free to contact me if you like this code.
 * -------------------------------------------------------------- */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace exMath.Common
{
    public class BasicOperations
    {
        /// <summary>
        /// Calculates the sum of all elements in the array.
        /// </summary>
        /// <param name="Values"></param>
        /// <returns></returns>
        public static double Sum(double[] Values)
        {
            double Sum = 0.0;

            foreach (double Value in Values)
                Sum += Value;

            return Sum;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="DomainValues"></param>
        /// <param name="Function"></param>
        /// <returns></returns>
        public static double Sum(double[] DomainValues, MathFunction Function)
        {
            double[] Results = Functions.ExecuteFunction(DomainValues, Function);
            return Sum(Results);      
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="DomainX"></param>
        /// <param name="DomainY"></param>
        /// <param name="Function"></param>
        /// <returns></returns>
        public static double Sum(double[] DomainX, double[] DomainY, MathFunction2 Function)
        {
            double[] Results = Functions.ExecuteFunction(DomainX, DomainY, Function);
            return Sum(Results);
        }

        /// <summary>
        /// Calculates the sum of Function results within x specified domain at x certain interval for domain values.
        /// </summary>
        /// <param name="Function"></param>
        /// <param name="DomainBegin"></param>
        /// <param name="DomainEnd"></param>
        /// <param name="Interval"></param>
        /// <returns></returns>
        public static double Sum(double DomainBegin, double DomainEnd, double Interval, MathFunction Function)
        {
            double[] DomainValues = Functions.ToDomainIntervalArray(DomainBegin, DomainEnd, Interval);
            return (Sum(DomainValues, Function));
        }

        /// <summary>
        /// Calculates x! (x * x - 1 * x - 2 * ... * 2 * 1)
        /// </summary>
        /// <param name="x"></param>
        /// <returns></returns>
        public static double Faculty(double Value)
        {
            double CurrentValue = 1.0;

            for (double i = Value; i > 1.0; i--)
            {
                CurrentValue *= i;
            }

            return CurrentValue;
        }

        /// <summary>
        /// Makes x positive number x negative one and vice versa.
        /// </summary>
        /// <param name="x"></param>
        /// <returns></returns>
        public static double FlipSign(double Value)
        {
            return -Value;
        }

        /// <summary>
        /// Squares the given number.
        /// </summary>
        /// <param name="x"></param>
        /// <returns></returns>
        public static double Square(double Value)
        {
            return Value * Value;
        }

        public static double Csc(double x)
        {
            return 1.0 / Math.Sin(x);
        }

        public static double Sec(double x)
        {
            return 1.0 / Math.Cos(x);
        }

        public static double Cot(double x)
        {
            return Math.Cos(x) / Math.Sin(x);
        }

        public static double Sec2(double x)
        {
            double value = Math.Tan(x + 1.0);
            return value * value;
        }

        public static double Csc2(double x)
        {
            double value = Cot(x);
            value = value * value;
            return 1.0 + value;
        }


    }
}
